<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 15-5-16
 * Time: 下午4:16
 */

class refund extends shopAdminBase{
    function __construct() {
        parent::__construct();
        $this->cms->setPageTitle("退货管理");
        $this->cms->setControlFile("shop/refund/refund_control.json");
    }

    function indexTask($page=1,$real_name,$username,$goods_name){
        $user_info=$this->user->getLoginInfo();
        $shop=ShopMode::init();
        $shop_id=$shop->getShopId($user_info['id']);
        $order=OrderMode::init();
        $goods=GoodsMode::init();
        $refund=OrderMode::init();
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $real_name=isset($real_name)?trim($real_name):null;
        $username=isset($username)?trim($username):null;
        $goods_name=isset($goods_name)?trim($goods_name):null;
        if(isset($real_name)&&!empty($real_name)){
            $user_id=UserMode::init()->getInfoByRealName($real_name)['user_id'];
            $user_id=isset($user_id)?$user_id:-1;
        }else if(isset($username)&&!empty($username)){
            $user_id=UserMode::init()->getIdByUsername($username);
            $user_id=isset($user_id)?$user_id:-1;
        }else{
            $user_id = null;
        }
//        if(isset($goods_name)&&!empty($goods_name)){
//            $shop_info=GoodsMode::init()->getList()
//        }
//
        list($r['refund'],$refund_total)=$refund->getRefundList($user_id,$shop_id,$countOfPage,$offset);
        $r['refund']=is_array($r['refund'])?$r['refund']:array();
        foreach($r['refund'] as &$v){
            $order_info=$order->getDetailWithoutJoin($v['order_id']);
            $goods_info=$goods->getSimpleInfo($order_info['goods_ids']);
            $v['order_id']=$goods_info['name'];
            $refund_user=$this->user->getDetail($v['user_id']);

            $r[$v['user_id']]['username']=$refund_user['username'];
            $r[$v['user_id']]['real_name']=UserMode::init()->getDetail($v['user_id'])['real_name'];
        }
        $url=WebRouter::init()->getQuestion("real_name={$real_name}&username={$username}&goods_name={$goods_name}&page=");
        $r['pages']=new Page($page,$refund_total,$countOfPage);
        $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        $r['refundType']=array(
            '0'=>"退款/退货",'1'=>"仅退款"
        );
        $r['currentState']=array(
            'create'=>"用户申请退款", 'apply'=>"商家同意退款", 'shipped'=>"用户已发货", 'confirm'=>"商家确认退款"
        );
        $this->cms->tableScene($r,"shop/refund/refund.php");
    }

    function applyTask($id){
        $refund=OrderMode::init();
        $s=$refund->setRefundState($id,'confirm');
        if($s){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="操作成功";
        }else{
            $r['message']="操作失败";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");

    }

    function applyWithGoodsTask($id){
        $refund=OrderMode::init();
        $s=$refund->setRefundState($id,'apply');
        if($s){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="操作成功";
        }else{
            $r['message']="操作失败";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");

    }

    function confirmTask($id){
        $refund=OrderMode::init();
        $s=$refund->setRefundConfirmState($id);
        if($s){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="操作成功";
        }else{
            $r['message']="操作失败";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }
} 